/*
 * Book.java
 */
package com.vs.kindlecoll.Beans;

import com.vs.kindlecoll.Util.HashGen;
import com.vs.MobiFile.MobiFile;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author volodymyr.sadovyy
 */
public class Book extends BookObject implements Transferable, Serializable, Comparable<Book> {

    private String title;
    private File location;
    private String kindleHash;
    private DataFlavor[] bookDataFlavors = new DataFlavor[]{new DataFlavor(Book[].class, "Book")};
    private boolean hidden;
    private List<BooksCollection> thisBookBelongsTo = new ArrayList<BooksCollection>();

    public Book() {
        location = null;
        kindleHash = null;
    }

    public Book(Book b) {
        title = b.title;
        location = b.location;
        kindleHash = b.kindleHash;

    }

    public Book(String t) {
        title = t;
        location = null;
        kindleHash = null;
    }

    //TODO: add correct title
    public Book(File f) throws IOException {
        title = new MobiFile(f).getTitle();
        kindleHash = HashGen.getHashString(f);
        location = f;
    }

    @Override
    public String toString() {
        //Cut extention from file name
        if (null != title && title.indexOf('.') > 0) {
            return title.substring(0, title.indexOf('.'));
            // return hash in case we do not have a name
        } else if (null == title && null != getKindleHash()) {
            return getKindleHash();
        } else {
            return title;
        }
    }

    public DataFlavor[] getTransferDataFlavors() {
        return bookDataFlavors;
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.equals(bookDataFlavors[0]);
    }

    public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
        if (flavor.equals(bookDataFlavors[0])) {
            return this;
        } else {
            throw new UnsupportedFlavorException(flavor);
        }
    }

    @Override
    public boolean equals(Object o) {
        if (o.getClass().equals(Book.class)) {
            if (null != this.getKindleHash()
                    && null != ((Book) o).getKindleHash()
                    && this.getKindleHash().equalsIgnoreCase(((Book) o).getKindleHash())) {
                return true;
            } else if (null != this.getTitle()
                    && null != ((Book) o).getTitle()
                    && this.getTitle().equalsIgnoreCase(((Book) o).getTitle())
                    && this.getTitle().equalsIgnoreCase(BooksCollection.EMPTY_COLL_MESS)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + (this.title != null ? this.title.hashCode() : 0);
        hash = 47 * hash + (this.getLocation() != null ? this.getLocation().hashCode() : 0);
        hash = 47 * hash + (this.getKindleHash() != null ? this.getKindleHash().hashCode() : 0);
        return hash;
    }

    @Override
    public String getPropertiesString() {
        return "title: " + title + "\n"
                + "location: " + (null == getLocation() ? null : getLocation().getAbsolutePath()) + "\n"
                + "kindleHash: " + getKindleHash() + "\n"
                + "collections[: " + thisBookBelongsTo + "]\n";
    }

    /**
     * @return the location
     */
    public File getLocation() {
        return location;
    }

    /**
     * @param location the location to set
     */
    public void setLocation(File location) {
        this.location = location;
        this.title = location.getName();
    }

    /**
     * @return the kindleHash
     */
    public String getKindleHash() {
        return kindleHash;
    }

    /**
     * @param kindleHash the kindleHash to set
     */
    public void setKindleHash(String kindleHash) {
        this.kindleHash = kindleHash;
    }

    public int compareTo(Book o) {
        final int BEFORE = -1;
        final int EQUAL = 0;

        if (this.equals(o)) {
            return EQUAL;
        }
        if (this == o) {
            return EQUAL;
        }

        if (this.getLocation() != null
                && o.getLocation() != null) {
            return this.getLocation().getName().compareTo(
                    o.getLocation().getName());
        }
        //By default let put ahead
        return BEFORE;

    }

    public String getTitle() {
        return title;
    }
    
    public void addCollection(BooksCollection bc){
        thisBookBelongsTo.add(bc);
    }
    
    public List<BooksCollection> getCollections(){
        return thisBookBelongsTo;
    }

	public void setTitle(String t) {
		title = t;
	}
            
    
}
